<?
    class Principio_Model_AccountLinkMapper extends Principio_Library_Mapper
    {
        private static $db_table = null;


        private static function getDBTable()
        {
            if (self::$db_table == null)
                self::$db_table = new Principio_Model_DbTable_AccountLink();

            return self::$db_table;
        }


        public static function delete($spa_id, $use_transaction = false)
        {
            try
            {
                if ($use_transaction)
                    self::getDBTable()->getAdapter()->beginTransaction();

                self::getDBTable()->delete(array("spa_id=?" => intval($spa_id)));

                if ($use_transaction)
                    self::getDBTable()->getAdapter()->commit();

                return true;
            }
            catch (Exception $ex)
            {
                $return = self::reportException($ex);

                if ($use_transaction)
                {
                    self::getDBTable()->getAdapter()->rollback();
                    return false;
                }
                else
                    return $return;

            };
        }


        /*
         * Set account_id = -1.
         */
        public static function reset($account_id)
        {
            self::getDBTable()->update(array("account_id" => "-1"), array("account_id=?" => $account_id));
        }


        public static function save(Principio_Model_AccountLink $model_accountlink)
        {
            try
            {
                for ($i = 0; $i < $model_accountlink->getExts(); $i++)
                {
                    if ($model_accountlink->getID($i) != -1)
                        self::getDBTable()->update(array("account_id" => $model_accountlink->getLinkAccountID($i), "ext_id" => $i), array("id=?" => $model_accountlink->getID($i)));
                    else
                        $model_accountlink->setID($i, self::getDBTable()->insert(array("account_id" => $model_accountlink->getLinkAccountID($i), "ext_id" => $i, "spa_id" => $model_accountlink->getSpaID())));
                }
            }
            catch (Exception $ex)
            {
                return self::reportException($ex);
            };
        }


        public static function getBySpaID($spa_id)
        {
            try
            {
                $account_links = array();

                $accountlinks_raw = self::getDBTable()->fetchAll(array("spa_id=?" => $spa_id));
                $account_links['spa_id'] = $spa_id;
                $account_links['accounts'] = array();

                foreach ($accountlinks_raw as $account_link)
                    $account_links['accounts'][] = array("id" => $account_link->id, "account_id" => $account_link->account_id, "ext_id=" => $account_link->ext_id);

                return (new Principio_Model_AccountLink($account_links));
            }
            catch (Exception $ex)
            {
                return self::reportException($ex);
            };
        }
    }
?>